﻿using DataServer.ApplicationLib.Services;
using DataServer.CommonLib.Constants;
using DataServer.CommonLib.Models.Dimension;
using Microsoft.AspNetCore.Mvc;

namespace Config.WebApi.Controllers
{
    public class DimensionController : ControllerBase
    {
        private readonly IDimensionService _dimensionService;

        public DimensionController(IDimensionService dimensionService)
        {
            _dimensionService = dimensionService;
        }

        [HttpGet(ApiRoutes.Dimension.QueryDimensions)]
        public async Task<IActionResult> QueryDimensions(DimensionSearchRequestDto? model)
        {
            if (model != null && (model.Page > 0 || model.Limit > 0))
            {
                var result = await _dimensionService.QueryDimensionsByPageAsync(model);
                return Ok(result);
            }
            else
            {
                var result = await _dimensionService.QueryDimensionsAsync(model);
                return Ok(result);
            }
        }

        [HttpPost(ApiRoutes.Dimension.CreateDimension)]
        public async Task<IActionResult> CreateDimension(DimensionEditRequestDto model)
        {
            var result = await _dimensionService.CreateDimensionAsync(model);
            return Ok(result);
        }

        [HttpGet(ApiRoutes.Dimension.GetDimension)]
        public async Task<IActionResult> GetDimension(Guid id)
        {
            var result = await _dimensionService.GetDimensionAsync(id);
            return Ok(result);
        }


        [HttpPut(ApiRoutes.Dimension.UpdateDimension)]
        public async Task<IActionResult> ModifyDimension(Guid id, [FromBody] DimensionEditRequestDto model)
        {
            var result = await _dimensionService.UpdateDimensionAsync(id, model);
            return Ok(result);
        }

        [HttpGet(ApiRoutes.Dimension.TestDimension)]
        public async Task<IActionResult> TestDimension(string dimensionCode, int limit)
        {
            var result = await _dimensionService.TestDimensionAsync(dimensionCode, limit);
            return Ok(result);
        }
    }
}